package io.renren.modules.course.controller;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.course.entity.CourseSmallEntity;
import io.renren.modules.course.service.CourseSmallService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2019-03-13 18:34:13
 */
@RestController
@RequestMapping("course/coursesmall")
public class CourseSmallController extends AbstractController {
    @Autowired
    private CourseSmallService courseSmallService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("course:coursesmall:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("userId",getUserId());
        PageUtils page = courseSmallService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("course:coursesmall:info")
    public R info(@PathVariable("id") Integer id){
        CourseSmallEntity courseSmall = courseSmallService.selectById(id);

        return R.ok().put("courseSmall", courseSmall);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("course:coursesmall:save")
    public R save(@RequestBody CourseSmallEntity courseSmall) throws UnsupportedEncodingException {

        Base64 base64 = new Base64();
        courseSmall.setCreateTime(new Date());
        courseSmall.setTeacherId(getUserId().intValue());

courseSmall.setTitle(courseSmall.getTitle().replaceAll("&amp;","&"));
        courseSmallService.insert(courseSmall);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("course:coursesmall:update")
    public R update(@RequestBody CourseSmallEntity courseSmall) throws UnsupportedEncodingException {
        Base64 base64 = new Base64();

//        courseSmall.setPicturePath(new String(base64.decode(courseSmall.getPicturePath()), "UTF-8"));
        ValidatorUtils.validateEntity(courseSmall);
        courseSmall.setTitle(courseSmall.getTitle().replaceAll("&amp;","&"));
        courseSmallService.updateAllColumnById(courseSmall);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/online")
//    @RequiresPermissions("course:coursesmall:delete")
    public R delete(@RequestBody Map map){
       // courseSmallService.deleteBatchIds(Arrays.asList(ids));
        courseSmallService.online(map);
        return R.ok();
    }


    /**
     * 模糊查询微课
     */
    @GetMapping("getCourseSmallByTitleLike")
    public R getCourseSmallClassificationByTitleLike(@RequestParam String word){
        List courseSmallList =  courseSmallService.getCourseSmallByTitleLike(word);

        return R.ok().put("data",courseSmallList);
    }

    /**
     * 根据id查询微课程
     */
    @GetMapping("getCourseSmallById")
    public R getCourseSmallById(@RequestParam int id){
       CourseSmallEntity courseSmallEntity =  courseSmallService.selectById(id);

        return R.ok().put("data",courseSmallEntity);
    }

    /**
     * 修改微课
     */
    @PostMapping("updateCourseSmall")
    public R updateCourseSmall(@RequestBody CourseSmallEntity courseSmallEntity){
        courseSmallService.updateAllColumnById(courseSmallEntity);

        return R.ok();
    }

    /**
     * 添加微课
     */
    @PostMapping("insertCourseSmall")
    public R insertCourseSmall(@RequestBody CourseSmallEntity courseSmallEntity){
        courseSmallService.insert(courseSmallEntity);

        return R.ok();
    }


    /**
     * 删除微课
     */
    @GetMapping("deleteCourseSmallById")
    public R deleteCourseSmallById(@RequestParam int id){

        courseSmallService.deleteById(id);

        return R.ok();
    }


}
